using System;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service.Template;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Exceptions;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Mutation;
using Me.Prettyprint.Hector.Api.Query;

namespace Me.Prettyprint.Cassandra.Service
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="N"></typeparam>
    /// <typeparam name="V"></typeparam>
	public class ColumnFamilyRowCopy<K,N,V>
	{
		protected ColumnFamilyRowCopy()
		{
			InitBlock();
		}
        public ColumnFamilyRowCopy(Keyspace keyspace, Serializer<K> keySerializer)
            : this()
        {
            Keyspace = keyspace;
            KeySerializer = keySerializer;
        }
		private void  InitBlock()
		{
            BytebufferSerializer = ByteBufferSerializer.Instance;
		}
        protected Keyspace Keyspace
        {
            get;
            set;
        }
        protected Serializer<K> KeySerializer
        {
            get;
            set;
        }
        protected ByteBufferSerializer BytebufferSerializer
        {
            get;
            set;
        }
        public K RowKey
        {
            get;
            set;
        }
        private K DestinationKey
        {
            get;
            set;
        }
        private string ColumnFamily
        {
            get;
            set;
        }
        public int MutateInterval
        {
            get;
            set;
        }
        public void copy() 
        {
            if (ColumnFamily == null)
            {
                throw new HectorException("Unable to clone row with null column family");
            }
            if (RowKey == null)
            {
                throw new HectorException("Unable to clone row with null row key");
            }
            if (DestinationKey == null)
            {
                throw new HectorException("Unable to clone row with null clone key");
            }
            ColumnFamilyTemplate<K, ByteBuffer> template = new ThriftColumnFamilyTemplate<K, ByteBuffer>(Keyspace, ColumnFamily, KeySerializer, BytebufferSerializer);
            Mutator<K> mutator = HFactory.CreateMutator<K,N,V>(Keyspace, KeySerializer, new BatchSizeHint(1, MutateInterval));
            ColumnFamilyUpdater<K, ByteBuffer> updater = template.CreateUpdater(DestinationKey, mutator);
            SliceQuery<K, ByteBuffer, ByteBuffer> query = HFactory.CreateSliceQuery(Keyspace, KeySerializer, BytebufferSerializer, BytebufferSerializer).
						SetColumnFamily(ColumnFamily).
						SetKey(RowKey);
            ColumnSliceEnumerator<K, ByteBuffer, ByteBuffer> iterator = new ColumnSliceEnumerator<K, ByteBuffer, ByteBuffer>(query, BytebufferSerializer.FromBytes(new byte[0])
                , BytebufferSerializer.FromBytes(new byte[0]), false);
            while (iterator.MoveNext()) 
            {
                HColumn<ByteBuffer, ByteBuffer> column = iterator.Current;
                updater.SetValue<ByteBuffer>(column.Name,column.Value,ByteBufferSerializer.Instance);
            }
            template.Update(updater);
        }
	}
}